<template>
  <div>
    <el-dialog
      :title="info.title"
      :visible.sync="info.isshow"
      @click="cancel"
      @close="close"
    >
      <el-form :model="formData" ref="formData" :rules="rules">
        <el-form-item label="上级分类" prop="pid" label-width="100px">
          <el-select v-model="formData.pid">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option
              v-for="(item, index) in catelist"
              :key="index"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称" prop="catename" label-width="100px">
          <el-input v-model="formData.catename"></el-input>
        </el-form-item>
        <el-form-item label="分类图片" prop="img" label-width="100px">
          <el-upload
            action="#"
            :auto-upload="false"
            list-type="picture-card"
            :on-remove="handleRemove"
            :file-list="fileList"
            :limit="1"
            :on-change="handleChange"
            :on-exceed="handleExceed"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="状态" prop="status" label-width="100px">
          <el-radio-group v-model="formData.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label-width="100px">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getCatelist, cateAdd, cateEdit } from "../../api";
export default {
  props: ["info"],
  data() {
    return {
      formData: {},
      dialogImageUrl: "",
      dialogVisible: false,
      rules: {
        catename: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
        pid: [{ required: true, message: "请选择上级分类", trigger: "blur" }],
      },
      catelist: [],
      fileList: [{ name: "", img: "" }],
    };
  },
  created() {
    this.getSelectData();
  },
  methods: {
    async getSelectData() {
      let res = await getCatelist();
      this.cateList = res.list;
    },
    handleRemove(file, fileList) {
       this.formData.img=''
      console.log(file, fileList);
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    handleChange(file) {
      this.formData.img = file.raw;
    },
    setValue(data) {
      this.formData = { ...data };
      this.fileList = [];
      let imgData = {
        name: "img",
        url: data.img,
      };
      if (data.img) {
        imgData.url = `http://localhost:3000${data.img}`;
        this.fileList.push(imgData);
      }
    },
    submitForm() {
      // this.isshow = false;
      this.$refs.formData.validate(async (valid) => {
        let fd = new FormData();
        for (const key in this.formData) {
          fd.append(key, this.formData[key]);
        }
        let res;
        if (valid) {
          if (this.formData.id) {
            //有id是修改数据
            res = await cateEdit(fd);
          } else {
            res = await cateAdd(fd);
          }
          if (res.code === 200) {
            this.$message({
              message: "操作成功",
              type: "success",
            });

            this.$emit("close");
            this.$emit("updata");
          }
        } else {
          console.log("err");
          return false;
        }
      });
    },
    cancel() {
      //   this.isshow = false;
      this.$emit("close");
    },
    close() {
      //   this.isshow = false;
      this.$emit("close");
    },
    edit() {
      this.info.isshow = true;
      this.info.title = "修改菜单";
      let data = data;
    },
  },
};
</script>
<style>
</style>